﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Petroleum.Tool;
using HZH_Controls.Controls;
using Petroleum.DB.Model;
using HZH_Controls.Forms;
using Petroleum.Model;
using Sunny.UI;
using Petroleum.UC.Public;
using System.Reflection;
using System.Data.SQLite;
using Newtonsoft.Json;

namespace Petroleum.UC
{
    public partial class Step9BitTypesDesign : UserControl
    {
        FormMain main = new FormMain();
        List<BitTypeDes> list = new List<BitTypeDes>();

        List<Casing> casingList = Session.CasingProgram.CasingList;
        Casing casing;
        public Step9BitTypesDesign(FormMain main)
        {
            InitializeComponent();
            this.main = main;
            ToolStripManager.Renderer = new HZH_Controls.Controls.ProfessionalToolStripRendererEx();
            ToolStripManager.Renderer = new ProfessionalToolStripRendererEx();
            InitHole();
        }
        void InitHole()
        {
            List<KeyValuePair<string, string>> spudCom = casingList.Select(m =>
                new KeyValuePair<string, string>(m.SpudNo, m.BitSize)
            ).ToList();
            cmdHole.Source = spudCom;
            cmdHole.SelectedIndex = 0;
            if (casingList.Count > 0)
            {
                casing = casingList[0];
                InitMainParam();
            }
            cmdHole.SelectedChangedEvent += CmdHole_SelectedChangedEvent;
        }
        private void CmdHole_SelectedChangedEvent(object sender, EventArgs e)
        { //获取对应的套管
            string spud = cmdHole.SelectedValue;
            casing = casingList.FirstOrDefault(m => m.SpudNo == spud);
            InitMainParam();
        }
        void InitMainParam()
        {
            if (casing != null)
            {
                if (casing.DrillingParametersList != null && casing.DrillingParametersList.Count > 0)
                {
                    lblwob.InputText = casing.DrillingParametersList[0].WOBkN;
                    lblrpm.InputText = casing.DrillingParametersList[0].RPM;
                }
            }
        }
        private void btnClear_BtnClick(object sender, EventArgs e)
        {
            foreach (Control item in groupLeft.Controls)
            {
                if (item is UCTextBoxEx)
                {
                    item.Text = "";
                }
            }
        }
        private void ucBtnExt2_BtnClick(object sender, EventArgs e)
        {
            this.verificationComponent1.Verification();
            if (lstErrorControl.Count == 0)
            {
                BitTypeDes bitTypeDes = new BitTypeDes()
                {
                    AmouthPCS = txtAmountPCS.InputText,
                    BitType = txtBitType.InputText,
                    HoleSize = cmdHole.TextValue,
                    Interval = txtIntervalMD1.InputText + "-" + txtIntervalMD2.InputText,
                    Notes = txtNode.InputText,
                    Footage = txtFootage.InputText,
                    WOB = lblwob.Text,
                    RPM = lblrpm.Text
                };
                list.Add(bitTypeDes);
                this.dgv.DataSource = list;
            }
            lstErrorControl.Clear();
            this.Invalidate(true);
        }
        private void dgv_Load(object sender, EventArgs e)
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "HoleSize", HeadText = "HoleSize", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "BitType", HeadText = "BitType", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "AmouthPCS", HeadText = "AmouthPCS", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Interval", HeadText = "Interval", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Notes", HeadText = "Notes", Width = 150, WidthType = SizeType.Absolute });
            this.dgv.Columns = lstCulumns;
        }
        private void btnDown_BtnClick(object sender, EventArgs e)
        {
            Session.BitTypeDes = list;
            main.AddControl(new Step10(main));
        }
        private void btnUp_BtnClick(object sender, EventArgs e)
        {
            main.AddControl(new Step8EstimatedMaterialConsumptionAndCostDesign(main));
        }
        List<Control> lstErrorControl = new List<Control>();
        private void verificationComponent1_Verificationed(VerificationEventArgs e)
        {
            if (!e.IsVerifySuccess)
            {
                lstErrorControl.Add(e.VerificationControl);
            }
        }
        private void StripMenuDel_Click(object sender, EventArgs e)
        {
            if (dgv.SelectRow != null)
            {
                int row = dgv.SelectRow.RowIndex;
                list.RemoveAt(row);
                this.dgv.DataSource = list;
                return;
            }
            FrmTips.ShowTipsWarning(this.FindForm(), "请先选择要删除的行");
        }
        //protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        //{
        //    //在检测到按Ctrl+V键后，系统无法复制多单元格数据，重写ProcessCmdKey方法，屏蔽系统粘贴事件，使用自定义粘贴事件，在事件中对剪贴板的HTML格式进行处理，获取表数据，更新DataGrid控件内容

        //    if (keyData == (Keys.V | Keys.Control) && this.dgv.SelectedCells.Count > 0)
        //    {
        //        DGVHelper.CopyData(this.dgv);
        //    }
        //    return base.ProcessCmdKey(ref msg, keyData);
        //}
        private void btnword_BtnClick(object sender, EventArgs e)
        {
            var depthListOld = main.projects.Where(n => n.TableName == TableNames.BitTypeDes.ToString()).FirstOrDefault();
            if (depthListOld != null)
            {
                Session.BitTypeDes = JsonConvert.DeserializeObject<List<BitTypeDes>>(depthListOld.TableData);
                this.list.Clear();
                list = Session.BitTypeDes;
                this.dgv.DataSource = list;
            }
        }
        private void btnsavetodb_BtnClick(object sender, EventArgs e)
        {
            Session.BitTypeDes = list;
            SQLiteDataReader reader = main.sqliteHelper.ReadFullTable("SessionData");
            var projects = EntityTranslate.Translate<SessionData>(reader);
            var BitTypeDes = projects.Where(n => n.TableName == TableNames.BitTypeDes.ToString()).FirstOrDefault();
            if (BitTypeDes != null)
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.BitTypeDes.ToString(), JsonConvert.SerializeObject(Session.BitTypeDes));
            else
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.BitTypeDes.ToString(), JsonConvert.SerializeObject(Session.BitTypeDes), 1);
            FrmTips.ShowTipsSuccess(new FormMain(), "数据保存成功！");
        }
        private void StripMenuEdit_Click(object sender, EventArgs e)
        {
            int row = dgv.SelectRow.RowIndex;
            var target = list[row];
            FrmInputs frm = new FrmInputs($"正在编辑{target.HoleSize}",
                         new string[] { "HoleSize", "BitType", "AmouthPCS", "Notes", "Footage" },
                         new Dictionary<string, HZH_Controls.TextInputType>() { },
                         //new Dictionary<string, HZH_Controls.TextInputType>() { { "Easting", HZH_Controls.TextInputType. }, { "Northing", HZH_Controls.TextInputType.Number }, { "TVDOfTarget", HZH_Controls.TextInputType.Number } },
                         new Dictionary<string, string>() { },
                         new Dictionary<string, KeyBoardType>() { },
                         new List<string>() { "HoleSize", "BitType", "AmouthPCS", "Notes", "Footage" }, new Dictionary<string, string> {
                          { "HoleSize",target.HoleSize },
                         { "BitType",target.BitType },
                         { "AmouthPCS",target.AmouthPCS},
                         { "Notes",target.Notes},
                         { "Footage",target.Footage},

                         });
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                target.HoleSize = frm.Values[0];
                target.BitType = frm.Values[1];
                target.AmouthPCS = frm.Values[2];
                target.Notes = frm.Values[3];
                target.Footage = frm.Values[4];
                list[row] = target;
                this.dgv.DataSource = list;
                FrmTips.ShowTipsInfo(this.FindForm(), $"更新成功");
            }
        }

    }
}
